package com.zzti.Uitl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class DBuitl {
	private static  String driver = "com.mysql.jdbc.Driver";
	static String username="root";
	static String password="123456";
	static String url="jdbc:mysql://localhost:3306/user";
	Connection conn;
	PreparedStatement pst;
	ResultSet rs;
	
	static {
	try {
		Class.forName(driver);
		System.out.println("驱动加载成功");
		
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		System.out.println("驱动加载失败");
	}	
	}
	public static Connection getConnection() throws SQLException{
		return DriverManager.getConnection(url, username,    
    			password); 
	}
	public static void release(ResultSet rs,PreparedStatement st,Connection conn){

		
		// TODO Auto-generated catch block
		
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(st!=null) {
			try {
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(rs!=null) {
			try {
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}	
		
}
	public Map<String, Object>  SelectAll(String sql, Object[] params) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			 conn=DBuitl.getConnection();
       pst=conn.prepareStatement(sql);
	
		  if (params != null) {    
              for (int i = 0; i < params.length; i++) {    
              	pst.setObject(i + 1, params[i]);    
              }    
          }    
              
          // 执行    
          rs = pst.executeQuery();    
          ResultSetMetaData md =  rs.getMetaData();
			int columns = md.getColumnCount();
          if(rs.next()) {    
          	
				for (int i = 0; i < columns; i++) {
					map.put(md.getColumnName(i+1), rs.getString(i+1));
				}   
          }    
              
      } catch (SQLException e) {    
          System.out.println(e.getMessage());    
      } finally {    
      	release(rs, pst, conn);    
      }    
  
      return map;  

}
	public int  Change(String sql, Object[] params) {
		int str=0;
	
		try {
			
			 conn=DBuitl.getConnection();
       pst=conn.prepareStatement(sql);
		
		  if (params != null) {    
              for (int i = 0; i < params.length; i++) {    
              	pst.setObject(i + 1, params[i]);    
              }    
          }    
              
          // 执行    
          str = pst.executeUpdate(sql);
         
              
      } catch (SQLException e) {    
          System.out.println(e.getMessage());    
      } finally {    
      	release(rs, pst, conn);    
      }    
  
      return str;  

}

}